package com.tywj.buscustomerapp.view.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.umeng.analytics.MobclickAgent;


import butterknife.BindView;

public class WebActivity extends BaseActivity {
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.webview)
    WebView webView;
    private LoadingDialog waitDialog;


    private String url = "http://www.cd917.com/app/main.html";
    @Override
    protected int getContentId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        showLoading();
        title.setText("胖哒直通车");
        WebSettings settings = webView.getSettings();
        settings.setTextZoom(100);
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setBuiltInZoomControls(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        int sdk = Build.VERSION.SDK_INT;
        if(sdk >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        if (getIntent()!=null){
            String code = getIntent().getStringExtra("url");
            webView.loadUrl(code);
        }else{
            webView.loadUrl(url);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                closeLoading();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if (url.equals("tel:4000-82-1717")){
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:4000-82-1717"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else if(url.startsWith("http:")||url.startsWith("https:")){
                    return false;
                }
                try{
                    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                    startActivity(intent);
                }catch(Exception e){
                    view.loadUrl(url);
                    return true;
                }
                return true;
            }
        });
//        showLoading();
//        title.setText("自由行");
//        if (getIntent()!=null){
//            String code = getIntent().getStringExtra("url");
//            if (code.equals("1") || code.equals("2")){
//                if (code.equals("2")) {
//                    String tel = SPUtils.getUserPhone(getApplicationContext());
//                    String encodeUrl = MD5Utils.getUrl(code);
//                    String apiKey = "DF93564D3B2B4DEBB8DF36A23DA69DDF";
//                    String sign = MD5Utils.getSign(tel, apiKey, encodeUrl,"1000");
//                    String codeUrl = "http://www.cd917.com/login/AgentUserLoginAndRedirect?Tel="
//                            + tel + "&ReturnUrl=" + encodeUrl + "&apiKey=" + apiKey + "&sign=" + sign;
//                    Log.i("taylor", codeUrl);
//                    webView.loadUrl(codeUrl);
//                }else if (code.equals("1")){
//                    urlcode = code;
//                    if (code1 == null || code1.equals("")) {
//                        code1 = "1000";
//                    }
//                    String tel = SPUtils.getUserPhone(getApplicationContext());
//                    String encodeUrl = MD5Utils.getUrl(urlcode);
//                    String apiKey = "DF93564D3B2B4DEBB8DF36A23DA69DDF";
//                    String sign = MD5Utils.getSign(tel, apiKey, encodeUrl, code1);
//                    String codeUrl = "http://www.cd917.com/login/AgentUserLoginAndRedirect?Tel="
//                            + tel + "&ReturnUrl=" + encodeUrl + "&apiKey=" + apiKey + "&recommendCode=" + code1 + "&sign=" + sign;
//                    Log.i("taylor", codeUrl);
//                    webView.loadUrl(codeUrl);
//                }
//            }else{
//                webView.loadUrl(code);
//            }
//        }else {
//            webView.loadUrl(url);
//        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();//返回上一页面
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    public void showLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(WebActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        if(build!=null) {
//            build.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                @Override
//                public void onDismiss(DialogInterface dialog) {
//                    if (code1 == null || code1.equals("")) {
//                        code1 = "1000";
//                    }
//                    String tel = SPUtils.getUserPhone(getApplicationContext());
//                    String encodeUrl = MD5Utils.getUrl(urlcode);
//                    String apiKey = "DF93564D3B2B4DEBB8DF36A23DA69DDF";
////                String apiKey = "82AFB673A47D471182E64A41CFEE4AAC";//测试
//                    String sign = MD5Utils.getSign(tel, apiKey, encodeUrl, code1);
//
////                String codeUrl = "http://beta.cd917.com/login/AgentUserLoginAndRedirect?Tel="
////                        + tel + "&ReturnUrl=" + encodeUrl + "&apiKey=" + apiKey +"&recommendCode="+code1 +"&sign=" + sign;
//                    String codeUrl = "http://www.cd917.com/login/AgentUserLoginAndRedirect?Tel="
//                            + tel + "&ReturnUrl=" + encodeUrl + "&apiKey=" + apiKey + "&recommendCode=" + code1 + "&sign=" + sign;
//                    Log.i("taylor", codeUrl);
//                    webView.loadUrl(codeUrl);
//                }
//            });
//        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("景区直通车");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("景区直通车");
        MobclickAgent.onPause(this);
    }
}
